// pages/index/component/recommendShop/recommendShop.js
import {nlist} from '../../../../api/shop';
import {list as tagList} from '../../../../api/tag';
import {recommendTagList} from '../../../../api/recommend';
const util = require('../../../../utils/util')
let app = getApp();

Component({
	/**
	 * 组件的属性列表
	 */
	properties: {
		info: {
			type: Object,
		  }
	},

	/**
	 * 组件的初始数据
	 */
	data: {
		shops: [],
		tagIds: [],
		pageNum:1,
		title:"附近推荐",
	},

	lifetimes: {
		attached() {
		  var arr = Object.keys(this.data.info.tagids)
		  if (arr.length > 0) {
			this.data.tagIds = this.data.info.tagids.split(',')
			console.log(this.data.tagIds)
			for (var i = 0; i < this.data.tagIds.length; i++) {
			  this.data.tagIds[i] = parseInt(this.data.tagIds[i])
			}
			this.setData({
			  tagIds: this.data.tagIds
			})
			// this.loadTag()
			// this.loadRecommendList()
			
		  }
		  this.loadShop()
		}
	  },

	/**
	 * 组件的方法列表
	 */
	methods: {
		loadTag(){
			let _this = this
			tagList({
				tagIds:this.data.tagIds
			}).then(res=>{
				console.info("标签列表---",res.data)
				console.info("标签列表---",res.data.data.length)
				console.info("标签列表---",res.data.data[0].name)
				if(res.data.data.length > 0){
					_this.setData({
						title:res.data.data[0].name
					})
				}			
			})
		},
		loadRecommendList(){
			let _this = this
			recommendTagList({
				tagIds:this.data.tagIds
			}).then(res=>{
				console.info("推荐列表信息---",res.data)
				res.data.data.forEach(item=>{
					let shopInfo =  item.shopVo
					shopInfo.logo = util.thumbnail(item.shopVo.logo,190,190)
					shopInfo.distance=util.rateConverter(item.shopVo.distance)
					// item.score=item.score.toFixed(1)
					shopInfo.score= '5.0'
					shopInfo.images=[]
					for(var i=0;i<item.score;i++){
						shopInfo.images.push('https://rzico.oss-cn-shenzhen.aliyuncs.com/dinnerBar/ic_list_star_red%402x.png')
					}
					// for(var k=item.images.length;k<5;k++){
					//   item.images.push('https://rzico.oss-cn-shenzhen.aliyuncs.com/dinnerBar/canyin/canyin-pingfen-gray.png')
					// }
					this.data.shops.push(shopInfo)
				  })
				  this.setData({
					shops:this.data.shops
				  })			
			})
		},
		onReachBottom() {
			console.info("上啦加载被触发了",this.data.pageNum)
			this.data.pageNum += 1
			this.loadShop()
		},
		loadShop(){
			nlist({
			  scopeType: 2,
			  pageNum:this.data.pageNum,
			//   shopCategoryId:this.data.shopCategoryId,
			  lng:app.globalData.lng,
			  lat:app.globalData.lat,
			  }).then(res=>{
				res.data.data.forEach(item=>{
				  item.logo = util.thumbnail(item.logo,190,190)
				  item.distance=util.rateConverter(item.distance*1000)
				  // item.score=item.score.toFixed(1)
				  item.score= '5.0'
				  item.images=[]
				  for(var i=0;i<item.score;i++){
					item.images.push('https://rzico.oss-cn-shenzhen.aliyuncs.com/dinnerBar/ic_list_star_red%402x.png')
				  }
				  // for(var k=item.images.length;k<5;k++){
				  //   item.images.push('https://rzico.oss-cn-shenzhen.aliyuncs.com/dinnerBar/canyin/canyin-pingfen-gray.png')
				  // }
				  this.data.shops.push(item)
				})
				this.setData({
				  shops:this.data.shops
				})
			})
		  },
	}
})
